package mspbots.tsheets.business.core;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Jay Yang
 */
@Log4j2
public abstract class AbstractToolsUtil {

    protected DatabaseClient databaseClient;

    protected ObjectMapper objectMapper;

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setDatabaseClient(DatabaseClient databaseClient) {
        this.databaseClient = databaseClient;
    }


    protected Long getLongValue(JsonNode node) {
        try {
            return ObjectUtils.isEmpty(node) ? null : node.asLong();
        } catch (Exception e) {
            log.debug(e.getMessage(), node);
            return null;
        }
    }

    protected Double getDoubleValue(JsonNode node) {
        try {
            return ObjectUtils.isEmpty(node) ? null : node.asDouble();
        } catch (Exception e) {
            log.debug(e.getMessage(), node);
            return null;
        }
    }

    protected Boolean getBooleanValue(JsonNode node) {
        try {
            return ObjectUtils.isEmpty(node) ? null : node.asBoolean();
        } catch (Exception e) {
            log.debug(e.getMessage(), node);
            return null;
        }
    }

    protected String getStringValue(JsonNode node) {
        try {
            return ObjectUtils.isEmpty(node) ? null : node.asText();
        } catch (Exception e) {
            log.debug(e.getMessage(), node);
            return null;
        }
    }

    protected Long getLongValueId(JsonNode node) {
        try {
            return ObjectUtils.isEmpty(node) ? null : node.get("id").asLong();
        } catch (Exception e) {
            log.debug(e.getMessage(), node);
            return null;
        }
    }

    protected String getStringValueName(JsonNode node) {
        try {
            return ObjectUtils.isEmpty(node) ? null : node.get("name").asText();
        } catch (Exception e) {
            log.debug(e.getMessage(), node);
            return null;
        }
    }

    protected String getStringValueIdentifier(JsonNode node) {
        try {
            return ObjectUtils.isEmpty(node) ? null : node.get("identifier").asText();
        } catch (Exception e) {
            log.debug(e.getMessage(), node);
            return null;
        }
    }

    protected LocalDateTime getDateTimeValue(JsonNode node) {
        try {
            String date = node.asText();
            if (date.length() == 10) {
                date = date + "T00:00:00Z";
            }
            return ObjectUtils.isEmpty(node) ? null : LocalDateTime.parse(date, DateTimeFormatter.ISO_DATE_TIME);
        } catch (Exception e) {
            return null;
        }
    }
}
